/**
 *  Copyright 2009 Henning Teek, Ingo Renner
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.googlecode.bricket.repository.jpa.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.synyx.hades.dao.GenericDao;
import org.synyx.hades.domain.Order;
import org.synyx.hades.domain.PageRequest;

import com.googlecode.bricket.domain.DomainObject;
import com.googlecode.bricket.repository.GenericDomainService;
import com.googlecode.bricket.repository.UncheckedRuntimeException;

/**
 * @author Ingo Renner
 * @author Henning Teek
 */
@Service(value="genericDomainService")
@Transactional(readOnly = true)
public class GenericDomainServiceImpl implements GenericDomainService {
	private final static Logger log = LoggerFactory.getLogger(GenericDomainServiceImpl.class);

	@Autowired
	private Map<String, GenericDao<? extends DomainObject, Long>> genericDaos;

	@Override
	public void init() {
		log.info("bricket daos = " + (genericDaos == null ? "null" : genericDaos.keySet()));
	}

	@SuppressWarnings("unchecked")
	private <T extends DomainObject> GenericDao<T, Long> getDao(Class<T> type) {
		String simpleName = type.getSimpleName();
		String key = simpleName.substring(0, 1).toLowerCase() + simpleName.substring(1) + "Dao";
		if (genericDaos.containsKey(key)) {
			return (GenericDao<T, Long>) genericDaos.get(key);
		}
		throw new UncheckedRuntimeException("no dao found for type: " + type.getName());
	}

	@Override
	public <T extends DomainObject> T load(Class<T> type, Long id) {
		return getDao(type).readByPrimaryKey(id);
	}

	@Override
	public <T extends DomainObject> List<T> loadAll(Class<T> type, final int from,
			final int count, final String sortProperty, final boolean sortAsc) {
		final PageRequest pageable;

		int page;

		if (from > 0) {
			page = from / count;
		} else {
			page = 0;
		}

		if (sortProperty == null || "".equals(sortProperty.trim())) {
			pageable = new PageRequest(page , count);
		} else {
			pageable = new PageRequest(page, count, sortAsc ? Order.ASCENDING : Order.DESCENDING, sortProperty);
		}
		return getDao(type).readAll(pageable).asList();
	}

	@Override
	public <T extends DomainObject> List<T> loadAll(Class<T> type) {
		return getDao(type).readAll();
	}

	@Override
	public <T extends DomainObject> int getCount(Class<T> type) {
		return getDao(type).count().intValue();
	}

	@Transactional(readOnly = false)
	@Override
	public <T extends DomainObject> void save(Class<T> type, T object) {
		getDao(type).save(object);
	}

	@Transactional(readOnly = false)
	@Override
	public <T extends DomainObject> void delete(Class<T> type, T object) {
		getDao(type).delete(getDao(type).readByPrimaryKey(object.getId()));
	}
}
